package com.jike.buyi.system.menu.service.impl;

import com.jike.buyi.system.index.domain.UserVo;
import com.jike.buyi.system.menu.dao.MenuDao;
import com.jike.buyi.system.menu.domain.MenuVo;
import com.jike.buyi.system.menu.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<MenuVo> queryByUser(UserVo userVo) {
        List<MenuVo> menuVos = menuDao.queryAll();
        Iterator<MenuVo> iter = menuVos.iterator();
        while(iter.hasNext()) {
            MenuVo vo = iter.next();
            for(MenuVo menuVo:menuVos){
                if(vo.getParentmenuid()!=null && vo.getParentmenuid().endsWith(menuVo.getMenuid())){
                    List<MenuVo> children = menuVo.getChildren();
                    if(children==null) children = new ArrayList<>();
                    children.add(vo);
                    menuVo.setChildren(children);
                    iter.remove();
                    break;
                }
            }
        }
        return menuVos;
    }

    @Override
    public List<MenuVo> queryAllByUser(UserVo userVo) {
        return menuDao.queryAll();
    }
}
